﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BaHuan.BusinessLayer;
using BaHuan.Common;
using BaHuan.Entities;

namespace BaHuan
{
    public partial class FrmThuTienHang : Form
    {
        private PhieuXuatHangManagement phieuxuathangMgt = new PhieuXuatHangManagement();
        private CTPhieuXuatHangManagement ctphieuxuathangMgt = new CTPhieuXuatHangManagement();
        private PhieuThuTienManagement phieuthutienMgt = new PhieuThuTienManagement();
        private ChiNhanhCongTyMgt chinhanhctyMgt = new ChiNhanhCongTyMgt();

        private NhanVien nhanvien;
        public NhanVien NhanVien
        {
            set { this.nhanvien = value; }
            get { return this.nhanvien; }
        }

        public FrmThuTienHang()
        {
            InitializeComponent();
        }

        private void FrmThuTienHang_Load(object sender, EventArgs e)
        {
            cboKhoXuat.DataSource = GetAllChiNhanhCongTy();
            cboKhoXuat.DisplayMember = "TenChiNhanh";
            cboKhoXuat.ValueMember = "Id";
        }


        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            DateTime dtTuNgay = dtpTuNgay.Value;
            DateTime dtDenNgay = dtpDenNgay.Value;
            int chinhanh = -1;
            if (cboKhoXuat.SelectedItem != null)
                chinhanh = ((ChiNhanhCongTy)cboKhoXuat.SelectedItem).Id;

            ResultObj result = new ResultObj();
            result = phieuxuathangMgt.GetAllPhieuXuatHang();
            
            List<PhieuXuatHang> lstPhieuXuatHang = (from pxh in (List<PhieuXuatHang>)result.Result
                                                    where pxh.MaSo.ToLower().Contains(txtTimKiem.Text.ToLower()) && 
                                                        pxh.NgayThang.Date >= dtTuNgay.Date && pxh.NgayThang.Date <= dtDenNgay.Date
                                                        && pxh.IdChiNhanhCongTy == chinhanh
                                                    select pxh).ToList();

            dgvDuyetPhieuXuatHang.AutoGenerateColumns = false;
            dgvDuyetPhieuXuatHang.DataSource = lstPhieuXuatHang;
        }

        private void cboKhoXuat_Leave(object sender, EventArgs e)
        {
            foreach (ChiNhanhCongTy b in cboKhoXuat.Items)
            {
                if (b.TenChiNhanh.ToLower().Contains(cboKhoXuat.Text) && cboKhoXuat.Text != "")
                {
                    cboKhoXuat.SelectedValue = b.Id;
                    break;
                }
            }

            if (cboKhoXuat.SelectedValue == null)
            {
                cboKhoXuat.Text = "";
                cboKhoXuat.Focus();
            }
        }

        private void dgvDuyetPhieuXuatHang_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvDuyetPhieuXuatHang.CurrentRow != null)
            {
                PhieuXuatHang phieuxuathang = (PhieuXuatHang)dgvDuyetPhieuXuatHang.CurrentRow.DataBoundItem;
                if (phieuxuathang != null)
                {
                    ResultObj result = new ResultObj();
                    result = phieuthutienMgt.GetAllPhieuThuTien();

                    List<PhieuThuTien> lstPhieuThuTien = (from c in (List<PhieuThuTien>)result.Result
                                                          where c.IdPhieuXuatHang == phieuxuathang.Id
                                                          select c).ToList();

                    long sotienPhieuThu = 0;
                    long sotienPhieuXuat = 0;
                    for (int i = 0; i < lstPhieuThuTien.Count; i++)
                    {
                        sotienPhieuThu += lstPhieuThuTien[i].SoTien;
                    }

                    sotienPhieuXuat += phieuxuathang.TongCong;
                    if (lstPhieuThuTien.Count > 0)
                    {
                        if (sotienPhieuThu >= sotienPhieuXuat)
                        {
                            MessageBox.Show("Khách hàng đã trả tiền đủ!", "Thông báo");
                        }
                        else
                        {
                            long sotien = sotienPhieuXuat - sotienPhieuThu;
                            FrmPhieuThuTien frmPhieuThuTien = new FrmPhieuThuTien(phieuxuathang, sotien, sotienPhieuThu, this.nhanvien);
                            frmPhieuThuTien.Show();
                        }
                    }
                    else
                    {
                        long sotien = sotienPhieuXuat - sotienPhieuThu;
                        FrmPhieuThuTien frmPhieuThuTien = new FrmPhieuThuTien(phieuxuathang, sotien, sotienPhieuThu, this.nhanvien);
                        frmPhieuThuTien.Show();
                    }
                }
            }
        }    

        public List<ChiNhanhCongTy> GetAllChiNhanhCongTy()
        {
            ResultObj resultObj = new ResultObj();
            resultObj = chinhanhctyMgt.GetAllChiNhanhCongTy();
            List<ChiNhanhCongTy> listChiNhanhCongTy = (from cn in(List<ChiNhanhCongTy>)resultObj.Result
                                                           where cn.IsDelete==0
                                                           select cn).ToList();

            return listChiNhanhCongTy;
        }     
    }
}
